package hello;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import video.MIDPPixelSource;
import video.VideoListener;



public class HelloVideo extends MIDlet  {

	public Canvas myCanvas;
	public MIDPPixelSource ps;
	
	public void startApp() throws MIDletStateChangeException {

		myCanvas = new VideoCanvas();
		ps = new MIDPPixelSource(160, 120);
	//	ps = new MIDPPixelSource(160, 120, null, myCanvas);
		Display display = Display.getDisplay(this);
		display.setCurrent(myCanvas);
		//ps = new MIDPPixelSource(160, 120,"http://itp.nyu.edu/~dbo3/cellphone/testvid/h.MPG",null);
		//ps = new MIDPPixelSource(160, 120,"http://java.sun.com/products/java-media/mma/media/test-mpeg.mpg",null);
		//ps = new MIDPPixelSource(160, 120,true);
		
		ps.addVideoListener(myCanvas, 15);

	}
 
	public void pauseApp() {
	}

	public void destroyApp(boolean arg0) throws MIDletStateChangeException {
		System.out.println("destroy");
	}

	public class VideoCanvas extends Canvas {
		
		int x;

		int y;

		Image myImage;



		public void keyPressed(int keyCode) {
			System.out.println(keyCode);
	
			System.out.println("Keycode" + keyCode);
	
			if (keyCode == Canvas.KEY_NUM4) {
				x--;
				ps.setLocationOnCanvas(x, 0);
			} else if (keyCode == Canvas.KEY_NUM6) {
				x++;
				ps.setLocationOnCanvas(x, 0);
			} else if (keyCode == Canvas.KEY_NUM0) {
				// thanks to stefan

				try {
					destroyApp(true);
				} catch (MIDletStateChangeException e) {
					e.printStackTrace();
				}

				notifyDestroyed();
			}
		

		}
		
		public void paint(Graphics g){
			if (ps.requiresManualPainting()){
				ps.grabFrame();
				Image snapshot = ps.getImage();
				g.drawImage(snapshot, x, y, 0);
			}

		}

	

	


	}

	

}
